package com.huikeportal.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.controller.common.Lang;
import com.huikeportal.controller.common.MessageManager;
import com.huikeportal.model.AspnetUsers;
import com.huikeportal.model.Language;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.PosAllInfo;
import com.huikeportal.model.PosDataManagerInfo;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteDataManagerInfo;
import com.huikeportal.model.SiteType;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.CommonService;
import com.huikeportal.service.InterFaceCommon;
import com.huikeportal.service.PosService;
import com.huikeportal.service.SiteService;

/**
 * Pos数据管理API
 * 
 * @author focus
 * 
 */
@Controller
public class PosAPI {

	@Autowired
	private PosService posService;
	
	@Autowired
	private InterFaceCommon interFaceCommon;
	
	@Autowired
	private CommonService commonService;
	

	/**
	 * 获取Pos数据信息
	 * 
	 * @param userId 用户ID
	 * @param square 广场编码
	 * @param siteType 场所类型
	 * @param siteName 场所名称
	 * @param beginTime 交易日期
	 * @param currentPage 查找的页数
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_getPosInfos", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getPosInfos(String userId,String beginTime, String square,String siteType,String siteName,
			String currentPage) throws Exception {
		
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getPosInfos");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		//userId验证
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		if(userId == null || "".equals(userId)){
			reMessage.append("userId为空错误！");
		}else{
			try {
				AspnetUsers aspnetUsers = commonService.getaspnetUsersMapper(userInfo);
				if(aspnetUsers == null){
					reMessage.append("userId不合法！");
				}else{
					userInfo.setUserName(aspnetUsers.getUsername());
				}
			} catch (Exception e) {
				reMessage.append("userId不合法！");
			}
		}
		
		if(beginTime == null || "".equals(beginTime)){
			reMessage.append("beginTime为空错误！");
		}
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			
			String permitSitesStr = getAllPermitSitesStr(userInfo);
			posDataManagerInfo.setSiteArea(permitSitesStr);
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			posDataManagerInfo.setBeginTime(sdf.parse(beginTime));
			posDataManagerInfo.setSquare(square);
			posDataManagerInfo.setSiteType(siteType);
			posDataManagerInfo.setSiteName(URLDecoder.decode(siteName, "UTF-8"));
			posDataManagerInfo.setSource(MessageManager.getMsg("selectSource"));
			
			if(!"-1".equals(square) && "300".equals(siteType)){
				posDataManagerInfo.setSiteKey(square);
				posDataManagerInfo.setSquare("-1");
			}
			
			PageDataVo pageDataVo = new PageDataVo();
			// 默认显示第一页
			if (currentPage == null || "".equals(currentPage)) {
				pageDataVo.setCurrentPage(1);
			} else {
				pageDataVo.setCurrentPage(Integer.parseInt(currentPage));
			}
			// 获取每页显示条数
			pageDataVo.setPageSize(Integer.parseInt(MessageManager
					.getMsg("pageSize")));
			// 设置开始显示条数
			pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
					* pageDataVo.getPageSize());
			// 设置参数
			pageDataVo.setSearchParameter(posDataManagerInfo);
			// 获取当前页用户信息
			pageDataVo = posService.getPosSiteList(pageDataVo);
			map.put("siteList", pageDataVo.getPageResult());// 结果集
			map.put("currentPage", pageDataVo.getCurrentPage());// 当前页
			map.put("maxPage", pageDataVo.getMaxPage());// 最大页
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(map);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
	}
	
	
	
	/**
	 * 获取POS数据明细信息
	 * 
	 * @param userId 用户ID
	 * @param transactionTime 交易时间
	 * @param siteKey 场所编码
	 * @param siteName 店铺名称
	 * @param currentPage 查找的页数
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_getPosDetailInfos", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getPosDetailInfos(String userId,String transactionTime, String siteKey,String siteName,
			String currentPage) throws Exception {
		
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getPosDetailInfos");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		//userId验证
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		if(userId == null || "".equals(userId)){
			reMessage.append("userId为空错误！");
		}else{
			try {
				AspnetUsers aspnetUsers = commonService.getaspnetUsersMapper(userInfo);
				if(aspnetUsers == null){
					reMessage.append("userId不合法！");
				}else{
					userInfo.setUserName(aspnetUsers.getUsername());
				}
			} catch (Exception e) {
				reMessage.append("userId不合法！");
			}
		}
		
		if(transactionTime == null || "".equals(transactionTime)){
			reMessage.append("beginTime为空错误！");
		}
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			
			String permitSitesStr = getAllPermitSitesStr(userInfo);
			posDataManagerInfo.setSiteArea(permitSitesStr);
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			
			posDataManagerInfo.setTransactionTime(sdf.parse(transactionTime));
			posDataManagerInfo.setSiteKey(siteKey);
			posDataManagerInfo.setSiteName(URLDecoder.decode(siteName, "UTF-8"));
			posDataManagerInfo.setSource(MessageManager.getMsg("selectSource"));
			
			PageDataVo pageDataVo = new PageDataVo();
			// 默认显示第一页
			if (currentPage == null || "".equals(currentPage)) {
				pageDataVo.setCurrentPage(1);
			} else {
				pageDataVo.setCurrentPage(Integer.parseInt(currentPage));
			}
			// 获取每页显示条数
			pageDataVo.setPageSize(Integer.parseInt(MessageManager
					.getMsg("pageSize")));
			// 设置开始显示条数
			pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
					* pageDataVo.getPageSize());
			// 设置参数
			pageDataVo.setSearchParameter(posDataManagerInfo);
			// 获取当前页用户信息
			pageDataVo = posService.getPosDetail(pageDataVo);
			map.put("posList", pageDataVo.getPageResult());// 结果集
			map.put("currentPage", pageDataVo.getCurrentPage());// 当前页
			map.put("maxPage", pageDataVo.getMaxPage());// 最大页
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(map);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
			
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
	}
	
	
	/**
	 * 新增Pos数据
	 * 
	 * @param siteKey 场所编码
	 * @param transactionNumber 交易笔数
	 * @param salesAmount 交易额
	 * @param posDate 交易时间
	 * @param details 明细
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_addPosInfo", produces = { "application/json;charset=UTF-8" })
	public String addPosInfo(String siteKey,String transactionNumber,String salesAmount,String details,
			String posDate)throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_addPosInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(siteKey == null || "".equals(siteKey)){
			reMessage.append("siteKey为空错误！");
		}
		
		if(transactionNumber == null || "".equals(transactionNumber)){
			reMessage.append("transactionNumber为空错误！");
		}
		
		if(salesAmount == null || "".equals(salesAmount)){
			reMessage.append("salesAmount为空错误！");
		}
		
		if(posDate == null || "".equals(posDate)){
			reMessage.append("posDate为空错误！");
		}
		
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			Map<String,Object> map = new HashMap<String,Object>();
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			posDataManagerInfo.setSiteKey(siteKey);
			posDataManagerInfo.setTransactionNumber(Integer.parseInt(transactionNumber));
			posDataManagerInfo.setSalesAmount(Double.parseDouble(salesAmount));
			posDataManagerInfo.setDetails(details);
			posDataManagerInfo.setInputType("1");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			posDataManagerInfo.setTransactionTime(sdf.parse(posDate));
			Map returnMap = posService.addPosData(posDataManagerInfo);
			
			map.put("posDataId", returnMap.get("posDataId"));
			map.put("siteKey", siteKey);
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(map);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	
	/**
	 * 修改Pos数据
	 * 
	 * @param id 记录标识
	 * @param siteKey 场所编码
	 * @param transactionNumber 交易笔数
	 * @param salesAmount 交易额
	 * @param details 明细
	 * @param posDate 交易时间
	 * @return 接口调用情况
	 */
	@RequestMapping(value = "/huike_updatePosInfo", produces = { "application/json;charset=UTF-8" })
	public String updatePosInfo(String id,String siteKey,String transactionNumber,String salesAmount,String details,
			String posDate)throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_updatePosInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(id == null || "".equals(id)){
			reMessage.append("id为空错误！");
		}
		
		if(siteKey == null || "".equals(siteKey)){
			reMessage.append("siteKey为空错误！");
		}
		
		if(transactionNumber == null || "".equals(transactionNumber)){
			reMessage.append("transactionNumber为空错误！");
		}
		
		if(salesAmount == null || "".equals(salesAmount)){
			reMessage.append("salesAmount为空错误！");
		}
		
		if(posDate == null || "".equals(posDate)){
			reMessage.append("posDate为空错误！");
		}
		
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			posDataManagerInfo.setId(Long.parseLong(id));
			posDataManagerInfo.setSiteKey(siteKey);
			posDataManagerInfo.setTransactionNumber(Integer.parseInt(transactionNumber));
			posDataManagerInfo.setSalesAmount(Double.parseDouble(salesAmount));
			posDataManagerInfo.setDetails(details);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			posDataManagerInfo.setTransactionTime(sdf.parse(posDate));
			MessageCommon messageCommon = posService.updatePosData(posDataManagerInfo);
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	/**
	 * 新增Pos数据明细
	 * 
	 * @param siteKey 场所编码
	 * @param transactionNumber 交易笔数
	 * @param salesAmount 交易额
	 * @param details 明细
	 * @param posDate 交易时间
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_addPosDetailInfo", produces = { "application/json;charset=UTF-8" })
	public String addPosDetailInfo(String siteKey,String transactionNumber,String salesAmount,
			String details,String transactionTime)throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_addPosDetailInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(siteKey == null || "".equals(siteKey)){
			reMessage.append("siteKey为空错误！");
		}
		
		if(transactionNumber == null || "".equals(transactionNumber)){
			reMessage.append("transactionNumber为空错误！");
		}
		
		if(salesAmount == null || "".equals(salesAmount)){
			reMessage.append("salesAmount为空错误！");
		}
		
		if(transactionTime == null || "".equals(transactionTime)){
			reMessage.append("transactionTime为空错误！");
		}
		
		
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			Map<String,Object> map = new HashMap<String,Object>();
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			posDataManagerInfo.setSiteKey(siteKey);
			posDataManagerInfo.setTransactionNumber(Integer.parseInt(transactionNumber));
			posDataManagerInfo.setSalesAmount(Double.parseDouble(salesAmount));
			posDataManagerInfo.setDetails(details);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			posDataManagerInfo.setTransactionTime(sdf.parse(transactionTime));
			Map returnMap = new HashMap();
			
			PosAllInfo dataInBaseInfo = posService.getDataBySiteKAndTransT(posDataManagerInfo);
			if(dataInBaseInfo!=null){
				dataInBaseInfo.setSitekey(posDataManagerInfo.getSiteKey());
				dataInBaseInfo.setTransactiontime(posDataManagerInfo.getTransactionTime());
				dataInBaseInfo.setTransactionnumber(posDataManagerInfo.getTransactionNumber());
				dataInBaseInfo.setSalesamount(posDataManagerInfo.getSalesAmount());
				dataInBaseInfo.setDetails(posDataManagerInfo.getDetails());
				posDataManagerInfo.setInputType("2");
				posService.updateDataOfImport(dataInBaseInfo);
			}else{
				posDataManagerInfo.setInputType("2");
				returnMap = posService.addPosDataOfImport(posDataManagerInfo);
			}

			
			map.put("posDataId", returnMap.get("posDataId"));
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
			
		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	
	/**
	 * 修改Pos数据明细
	 * 
	 * @param id 记录ID
	 * @param siteKey 场所编码
	 * @param transactionNumber 交易笔数
	 * @param salesAmount 交易额
	 * @param details 明细
	 * @param posDate 交易时间
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_updatePosDetailInfo", produces = { "application/json;charset=UTF-8" })
	public String updatePosDetailInfo(String id,String siteKey,String transactionNumber,String salesAmount,
			String details,String transactionTime)throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_updatePosDetailInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(id == null || "".equals(id)){
			reMessage.append("id为空错误！");
		}
		
		if(siteKey == null || "".equals(siteKey)){
			reMessage.append("siteKey为空错误！");
		}
		
		if(transactionNumber == null || "".equals(transactionNumber)){
			reMessage.append("transactionNumber为空错误！");
		}
		
		if(salesAmount == null || "".equals(salesAmount)){
			reMessage.append("salesAmount为空错误！");
		}
		
		if(transactionTime == null || "".equals(transactionTime)){
			reMessage.append("transactionTime为空错误！");
		}
		
		
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			posDataManagerInfo.setId(Long.parseLong(id));
			posDataManagerInfo.setTransactionNumber(Integer.parseInt(transactionNumber));
			posDataManagerInfo.setSalesAmount(Double.parseDouble(salesAmount));
			posDataManagerInfo.setDetails(details);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			posDataManagerInfo.setTransactionTime(sdf.parse(transactionTime));
			MessageCommon messageCommon = posService.updatePosDataDetail(posDataManagerInfo);
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();

		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	
	/**
	 * 删除POS数据（根据场所编码和交易时间）
	 * 
	 * @param siteKey 场所编码
	 * @param transactionTime 交易时间
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_deletePosInfo", produces = { "application/json;charset=UTF-8" })
	public String deletePosInfo(String siteKey,String transactionTime)throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_deletePosInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(siteKey == null || "".equals(siteKey)){
			reMessage.append("siteKey为空错误！");
		}
		
		if(transactionTime == null || "".equals(transactionTime)){
			reMessage.append("transactionTime为空错误！");
		}
		
		
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			
			HashMap<String,String> map = new HashMap<String,String>();
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			posDataManagerInfo.setSiteKey(siteKey);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			posDataManagerInfo.setTransactionTime(sdf.parse(transactionTime));
			MessageCommon messageCommon = posService.deletePosData(posDataManagerInfo);
			map.put("flag", String.valueOf(messageCommon.isFlag()));
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
			
			
		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	/**
	 * 删除POS数据明细（根据Id）
	 * 
	 * @param id 记录ID
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_deletePosDetailInfo", produces = { "application/json;charset=UTF-8" })
	public String deletePosDetailInfo(String id)throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_deletePosDetailInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(id == null || "".equals(id)){
			reMessage.append("siteKey为空错误！");
		}
		
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			HashMap<String,String> map = new HashMap<String,String>();
			PosDataManagerInfo posDataManagerInfo = new PosDataManagerInfo();
			posDataManagerInfo.setId(Long.parseLong(id));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			MessageCommon messageCommon = posService.deletePosDataDetail(posDataManagerInfo);

			
			map.put("flag", String.valueOf(messageCommon.isFlag()));
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	
	
	
	
	
	// 获取该用户所拥有的所有的店铺权限
	private String getAllPermitSitesStr(UserInfo userInfo) throws Exception{
		String permitSitesStr = "";
		
		// lowPermission < 300 的是广场以上的权限
		List<SiteAllInfo> permitSiteList = posService.getSitesByUser(userInfo);
		List<SiteAllInfo> allSiteList = new ArrayList<SiteAllInfo>();
		int lowPermission = posService.getLowPermission(permitSiteList);
		if(lowPermission < 300){
			List<SiteAllInfo> allGcList = posService.getAllGcList(permitSiteList);
			allSiteList = posService.getAllSiteList(allGcList);
			allSiteList.addAll(allGcList);
		}else if(lowPermission > 500){
			allSiteList = permitSiteList;
		}else{
			allSiteList = posService.getAllSiteList(permitSiteList);
		}
		permitSitesStr = posService.getSitePermitAreaStr(allSiteList);
		return permitSitesStr;
	}
	
}
